package com.clas.liu.myapplication.view.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.clas.liu.myapplication.R;

/**
 * Created by liu on 2019/2/27.
 */

public class MainTabButton extends RelativeLayout{

    private ImageView lv;
    private TextView redCount;
    private TextView point;

    public MainTabButton(Context context) {
        super(context);
    }

    public MainTabButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.main_tab_buttom,this);
        findView();
        setTypedArrayValue(context,attrs);
    }


    public MainTabButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    private void setTypedArrayValue(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.main_tab_button);
        int lvSrc = typedArray.getResourceId(R.styleable.main_tab_button_lv_src,0);
        typedArray.recycle();
        lv.setImageResource(lvSrc);
        redCount.setVisibility(GONE);
        point.setVisibility(GONE);
    }
    public void showCount(int count){
        if (count == 0) {
            closeRed();
            return;
        }
        point.setVisibility(GONE);
        if(redCount.getVisibility() == GONE)
            redCount.setVisibility(VISIBLE);
        redCount.setText(String.valueOf(count));
    }
    public void showPoint(){
        redCount.setVisibility(GONE);
        point.setVisibility(VISIBLE);
    }
    public void closeRed(){
        if(point.getVisibility() == VISIBLE)
            point.setVisibility(GONE);
        if(redCount.getVisibility() == VISIBLE)
            redCount.setVisibility(GONE);
    }
    public ImageView getLv(){
        if(lv!=null)
            return lv;
        return null;
    }
    public void setSelected(boolean isSelected,int position){
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)lv.getLayoutParams();
        if(isSelected){
            params.width *=1.3;
            params.height *=1.3;
        }else{
            params.width /=1.3;
            params.height /=1.3;
        }
        lv.setLayoutParams(params);
        switch (position){
            case 0:
                if (isSelected)
                    lv.setImageResource(R.mipmap.home);
                else
                    lv.setImageResource(R.mipmap.home_normal);
                break;
            case 1:
                if (isSelected)
                    lv.setImageResource(R.mipmap.square);
                else
                    lv.setImageResource(R.mipmap.square_normal);
                break;
            case 2:
                if (isSelected)
                    lv.setImageResource(R.mipmap.chat);
                else
                    lv.setImageResource(R.mipmap.chat_normal);
                break;
            case 3:
                if (isSelected)
                    lv.setImageResource(R.mipmap.user);
                else
                    lv.setImageResource(R.mipmap.user_normal);
                break;
        }
    }

    private void findView() {
        lv = findViewById(R.id.main_tab_lv);
        redCount = findViewById(R.id.main_tab_red_count);
        point = findViewById(R.id.main_tab_red_point);
    }

}
